package china.accountinghelper.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xdroid.base.AdapterExpandableListBase;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import china.wc.tallyhelper.R;
import china.wc.tallyhelper.business.BusinessExpense;
import china.wc.tallyhelper.business.BusinessExpenseDetail;
import china.wc.tallyhelper.model.ModelBook;
import china.wc.tallyhelper.model.ModelExpense;
import china.wc.tallyhelper.model.ModelExpenseDetail;
import com.xdroid.util.DateTimeUtil;

public class AdapterExpandableListViewBookDetail extends AdapterExpandableListBase<ModelExpense, ModelExpenseDetail>
{
    private BusinessExpense mBusinessExpense = new BusinessExpense();
    private BusinessExpenseDetail mBusinessExpenseDetail = new BusinessExpenseDetail();
    private ModelBook mBook;

    public AdapterExpandableListViewBookDetail(Context pContext, ModelBook pBook)
    {
        super(pContext, null, null);
        mBook = pBook;
        init(mBook);
    }

    @Override
    public long getGroupId(int pGroupPosition)
    {
        return getGroup(pGroupPosition).getID();
    }

    @Override
    public long getChildId(int pGroupPosition, int pChildPosition)
    {
        return getChild(pGroupPosition, pChildPosition).getID();
    }

    @Override
    public boolean hasStableIds()
    {
        return true;
    }

    @Override
    public boolean isChildSelectable(int pGroupPosition, int pChildPosition)
    {
        return false;
    }

    @Override
    public void notifyDataSetChanged()
    {
        init(mBook);
        super.notifyDataSetChanged();
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    {
        if (convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.item_expandablelistview_bookdetail_expense, null);
        }
        
        ModelExpense _Expense = getGroup(groupPosition);
        RelativeLayout _ItemExpandableListViewBookDetailExpense_RelativeLayout_Tag = (RelativeLayout) convertView.findViewById(R.id.ItemExpandableListViewBookDetailExpense_RelativeLayout_Tag);
        ImageView _ItemExpandableListViewBookDetailExpense_ImageView_Arrow = (ImageView) convertView.findViewById(R.id.ItemExpandableListViewBookDetailExpense_ImageView_Arrow);
        TextView _ItemExpandableListViewBookDetailExpense_TextView_Date = (TextView) convertView.findViewById(R.id.ItemExpandableListViewBookDetailExpense_TextView_Date);
        TextView _ItemExpandableListViewBookDetailExpense_TextView_CountSum = (TextView) convertView.findViewById(R.id.ItemExpandableListViewBookDetailExpense_TextView_CountSum);
        TextView _ItemExpandableListViewBookDetailExpense_TextView_Category = (TextView) convertView.findViewById(R.id.ItemExpandableListViewBookDetailExpense_TextView_Category);
        TextView _ItemExpandableListViewBookDetailExpense_TextView_Payer = (TextView) convertView.findViewById(R.id.ItemExpandableListViewBookDetailExpense_TextView_Payer);
        TextView _ItemExpandableListViewBookDetailExpense_TextView_ExpenseMoney = (TextView) convertView.findViewById(R.id.ItemExpandableListViewBookDetailExpense_TextView_ExpenseMoney);
        TextView _ItemExpandableListViewBookDetailExpense_TextView_Comment = (TextView) convertView.findViewById(R.id.ItemExpandableListViewBookDetailExpense_TextView_Comment);
        
        if (groupPosition == 0) {
            _ItemExpandableListViewBookDetailExpense_RelativeLayout_Tag.setVisibility(View.VISIBLE);
        } else {
            String _ExpenseDate = DateTimeUtil.getYearMonthDate1(_Expense.getExpenseDate());
            String _PreviousExpenseDate = DateTimeUtil.getYearMonthDate1(getGroup(groupPosition-1).getExpenseDate());
            if (!_ExpenseDate.equals(_PreviousExpenseDate)) {
                _ItemExpandableListViewBookDetailExpense_RelativeLayout_Tag.setVisibility(View.VISIBLE);
            } else {
                _ItemExpandableListViewBookDetailExpense_RelativeLayout_Tag.setVisibility(View.GONE);
            }
        }
        
        if (isExpanded) {
            _ItemExpandableListViewBookDetailExpense_ImageView_Arrow.setImageResource(R.drawable.go_detail);
        } else {
            _ItemExpandableListViewBookDetailExpense_ImageView_Arrow.setImageResource(R.drawable.go_detail_for_account);
        }
        
        if (_Expense.getComment() == null || "".equals(_Expense.getComment())) {
            _ItemExpandableListViewBookDetailExpense_TextView_Comment.setVisibility(View.GONE);
        } else {
            _ItemExpandableListViewBookDetailExpense_TextView_Comment.setVisibility(View.VISIBLE);
            _ItemExpandableListViewBookDetailExpense_TextView_Comment.setText(_Expense.getComment());
        }
        
        _ItemExpandableListViewBookDetailExpense_TextView_Date.setText(DateTimeUtil.getYearMonthDate2(_Expense.getExpenseDate()));
        _ItemExpandableListViewBookDetailExpense_TextView_CountSum.setText(String.format(getContext().getString(R.string.message_expense_sum), mBusinessExpense.count(mBook, _Expense.getExpenseDate())));
        _ItemExpandableListViewBookDetailExpense_TextView_Category.setText(mBusinessExpense.getCateoryName(_Expense));
        _ItemExpandableListViewBookDetailExpense_TextView_Payer.setText(mBusinessExpense.getPayerName(_Expense));
        _ItemExpandableListViewBookDetailExpense_TextView_ExpenseMoney.setText(String.format(getContext().getString(R.string.message_expense), mBusinessExpenseDetail.count(_Expense)));
        
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
    {
        if (convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.item_expandablelistview_bookdetail_expensedetail, null);
        }
        ModelExpenseDetail _ExpenseDetail = getChild(groupPosition, childPosition);
        TextView _ItemExpandableListViewBookDetailExpenseDetail_TextView_Consumer = (TextView) convertView.findViewById(R.id.ItemExpandableListViewBookDetailExpenseDetail_TextView_Consumer);
        TextView _ItemExpandableListViewBookDetailExpenseDetail_TextView_Money = (TextView) convertView.findViewById(R.id.ItemExpandableListViewBookDetailExpenseDetail_TextView_Money);
        
        _ItemExpandableListViewBookDetailExpenseDetail_TextView_Consumer.setText(mBusinessExpenseDetail.getConsumerName(_ExpenseDetail));
        _ItemExpandableListViewBookDetailExpenseDetail_TextView_Money.setText(String.format(getContext().getString(R.string.message_expense), _ExpenseDetail.getMoney2()));
        
        return convertView;
    }

    /**
     * 初始化数据源。
     * @param pBook
     */
    private void init(ModelBook pBook)
    {
        List<ModelExpense> _GroupList = mBusinessExpense.select(pBook);
        
        
        Map<ModelExpense, List<ModelExpenseDetail>> _ChildMap = new HashMap<ModelExpense, List<ModelExpenseDetail>>();
        for (ModelExpense _TemExpense : _GroupList) {
            List<ModelExpenseDetail> _ExpenseDetails = mBusinessExpenseDetail.select(_TemExpense);
            _ChildMap.put(_TemExpense, _ExpenseDetails);
        }
        
        setGroupList(_GroupList);
        setChildMap(_ChildMap);
    }

}
